<div class="tmd-doc">
<p></p>
<h1 class="tmd-header-1">
map 1
</h1>
<p></p>
<div id="question" class="tmd-base">
<div class="tmd-usual">
What does the following program print?
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

func main() {
	m := make(map[string]int, 3)
	x := len(m)
	m["Go"] = m["Go"]
	y := len(m)
	println(x, y)
}
</code></pre>
</div>
<p></p>
<div class="tmd-usual">
Choices:
</div>
<p></p>
<input type="radio" id="choiceA" name="choice" class="choice">
<input type="radio" id="choiceB" name="choice" class="choice">
<input type="radio" id="choiceC" name="choice" class="choice">
<input type="radio" id="choiceD" name="choice" class="choice">

<div id="choices">

<ul style="list-style-type:none;">
<li><label for="choiceA">3 3</label></li>
<li><label for="choiceB">3 4</label></li>
<li><label for="choiceC">0 0</label></li>
<li><label for="choiceD">0 1</label></li>
</ul>

</div><p></p>
<div id="answer" class="tmd-base">
<div class="tmd-usual">
Answer: 0 1
</div>
<p></p>
<div class="tmd-usual">
Run it on <a href="https://go.dev/play/p/MwomJeUCT9o">Go play</a>.
</div>
<p></p>
<div class="tmd-usual">
Key points:
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
when using the <code class="tmd-code-span">make</code> function to create a map, the second argument is neither the initial length nor the capacity of the result map. It is just a hint for Go runtime to allocate a large enough backing array to hold at least the specified number of entries.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
The length (number of entries) of the result map is zero. <code class="tmd-code-span">m["Go"] = m["Go"]</code> is equivalent to <code class="tmd-code-span">m["Go"] = 0</code>. After the assignment, the map contains one entry.
</div>
</li>
</ul>
<p></p>
</div>
<p></p>
</div>
